require('./index.css')
require('page/common/top/index.js');
var _util = require('util/util.js')
var listTemplate = require('./index.string');
var _cart_service = require('service/cart-service.js')



var cart = {
    cartItemData : {
        titile      : '',
        cartItemList : {},
        totalCost : ''
    },
    init : function(){
        this.loadAccountInfo();
        // this.bindEvents();
        return this;
    },
    bindEvents : function(){
        $('.remove-btn').on("click",function(){
            var itemId = $(this).parent().parent().children('td').first().children().first().text();
            _cart_service.removeCartItem(
                itemId,
                function(res){
                    console.log("xixi")
                    window.location.href ='./cart.html';
                },
                function(err){
                    console.log("ERROR...")
                }
            )
        }),
        $('.quantityInput').on("blur",function(){
            var itemId = $(this).parent().parent().children('td').first().children().first().text();
            var num = $(this).val();
            console.log(num)
            console.log(itemId)
            _cart_service.updateCart(
                itemId,
                num,
                function(res){
                    window.location.href ='./cart.html';
                },
                function(err){
                    console.log("ERROR...")
                }
            )
        })
    },
    loadAccountInfo : function(){
        var _this = this;
        
        _cart_service.getCartItemList(
            function(res){
                console.log(res)
                _this.cartItemData.titile = 'Shopping Cart';
                _this.cartItemData.cartItemList = res.itemVOList;
                _this.cartItemData.totalCost = res.totalCost;
                var result = _util.renderHtml(listTemplate,{cartItemData : _this.cartItemData})
                console.log(result);
                $('.Cart').html(result);
                _this.bindEvents();
            },
            function(res){
                console.log("ERROR..*..")
            }
        )
    }
};

module.exports = cart.init()